//
//  ImageBtn.m
//  智能备考学习
//
//  Created by 盛超 on 2018/1/19.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "DicImageBtn.h"
#import "NSString+DicUtils.h"
#define imageWidth 14

@implementation DicImageBtn {
    UILabel *titleLabel;
    UIImageView *imageView;
    CGFloat margin;
    CGFloat dic_font;
    UIImage *dic_image;
    NSString *dic_title;
    ImgPosition dic_Position;
}
- (id)initWithTitle:(NSString *)title image:(UIImage *)image ImagePosition:(ImgPosition)position {
    if (self = [super init]) {
        margin = 5;
        dic_font = 13.0f;
        dic_image = image;
        dic_title = title;
        dic_Position = position;
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:titleLabel];
        titleLabel.attributedText = [title dic_HtmlStringWithFontSize:dic_font];
        titleLabel.numberOfLines = 0;
        titleLabel.textColor = [UIColor grayColor];
        
        imageView = [[UIImageView alloc] init];
        imageView.image = image;
        [self addSubview:imageView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame title:(NSString *)title image:(UIImage *)image ImagePosition:(ImgPosition)position {
    self = [super initWithFrame:frame];
    if (self) {
        margin = 5;
        dic_font = 12.0f;
        dic_image = image;
        dic_title = title;
        dic_Position = position;
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:titleLabel];
        titleLabel.attributedText = [title dic_HtmlStringWithFontSize:dic_font];
       // titleLabel.numberOfLines = 0;
        titleLabel.textColor = [UIColor grayColor];
        
        imageView = [[UIImageView alloc] init];
        imageView.image = image;
        [self addSubview:imageView];
        
        CGSize size= [titleLabel.attributedText boundingRectWithSize:CGSizeMake(MAXFLOAT, frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        //假设title与图片、按钮边缘间隔都是margin,图片大小imageWidth*imageWidth
//        if (size.width>self.frame.size.width-margin*2-imageWidth-margin) {
//            size.width = self.frame.size.width-margin*2-imageWidth-margin;
//        }
        
        if (position == DicImgInRight) {
            
            titleLabel.frame = CGRectMake(margin, 0, size.width+5, self.frame.size.height);
            
            imageView.frame = CGRectMake(titleLabel.frame.size.width+ titleLabel.frame.origin.x+margin, (self.frame.size.height-imageWidth)/2, imageWidth, imageWidth);
            
            
        } else {
            
            imageView.frame = CGRectMake(margin, (self.frame.size.height-imageWidth)/2, imageWidth, imageWidth);

            titleLabel.frame = CGRectMake(margin+imageView.frame.origin.x+imageView.frame.size.width, 0, size.width+5, self.frame.size.height);
            
        }
        
    }
    
    return self;
}

- (void)resetTitle:(NSString *)title Image:(UIImage *)image ImagePosition:(ImgPosition)position
{
    titleLabel.attributedText = [title dic_HtmlStringWithFontSize:dic_font];
    imageView.image = image;
    
    CGSize size= [titleLabel.attributedText boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    //假设lb_title与图片、按钮边缘间隔都是margin,图片大小imageWidth*imageWidth
//    if (size.width>self.frame.size.width-margin*2-imageWidth-margin) {
//        size.width =self.frame.size.width-margin*2-imageWidth-margin;
//    }
    
    if (position == DicImgInRight) {

        titleLabel.frame = CGRectMake(margin, 0, size.width+5, self.frame.size.height);

        imageView.frame = CGRectMake(titleLabel.frame.size.width+ titleLabel.frame.origin.x+margin, (self.frame.size.height-imageWidth)/2, imageWidth, imageWidth);
        CGRect rect = CGRectMake(self.frame.origin.x, self.frame.origin.y, titleLabel.frame.size.width+ titleLabel.frame.origin.x+margin + imageWidth, self.frame.size.height);
        self.frame = rect;
        NSLog(@"%@,frame:%@",titleLabel.text,NSStringFromCGRect(rect));
        
        titleLabel.textColor = [UIColor grayColor];

    } else {

        imageView.frame = CGRectMake(margin, (self.frame.size.height-imageWidth)/2, imageWidth, imageWidth);

        titleLabel.frame = CGRectMake(margin+imageView.frame.origin.x+imageView.frame.size.width,0, size.width+5, self.frame.size.height);
        
        CGRect rect = CGRectMake(self.frame.origin.x, self.frame.origin.y,margin+imageView.frame.origin.x+imageView.frame.size.width, self.frame.size.height);
        self.frame = rect;
        
        titleLabel.textColor = [UIColor grayColor];;
    }
}

#pragma mark - 改变图片/文字
- (void)setImage:(UIImage *)image {
    _image = image;
    dic_image = image;
    [self resetTitle:dic_title Image:image ImagePosition:dic_Position];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    dic_title = title;
    [self resetTitle:title Image:dic_image ImagePosition:dic_Position];
}

#pragma mark - 动态图片
- (void)setAnimationImages:(NSArray *)animationImages {
    _animationImages = animationImages;
}

- (void)setBecomeHightlightly:(BOOL)becomeHightlightly {
    if (becomeHightlightly) {
        titleLabel.alpha = 0.5;
        imageView.alpha = 0.5;
    } else {
        titleLabel.alpha = 1;
        imageView.alpha = 1;
        imageView.animationImages = _animationImages;
        imageView.animationDuration = 0.5;
        imageView.animationRepeatCount = 0;
        [imageView startAnimating];
    }
}

- (void)stopPlayAnimation {
    [imageView stopAnimating];
}

@end
